package cn.jdx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller
@RestController //表示当前控制器下的所有返回string类型的方法都不会被视图解析器解析,直接返回字符串
public class PController {

    //@ResponseBody //表示当前的方法不会被视图解析器解析，会直接返回一个字符串到前端
    //produces解决响应体返回类型和编码问题
    @RequestMapping(value = "/json",produces = "application/json;charset=utf-8")
    public String getJson() throws JsonProcessingException {
        P p = new P("张三",18,"男");
        System.out.println("[信息]:"+"调用了"+p.toString());
        //返回json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(p);
    }


}
